//
//  ActivityModifier.swift
//  BasicTools
//
//  Created by ikang on 2025/8/28.
//

import SwiftUI

#if os(iOS)
/**
 ShareLink(item: URL(string: "https://apps.apple.com/us/app/id1631264265")!) { 
     Text("123")
 }
 */

// UIHostingController(rootView: <#T##Content#>)
// UIHostingConfiguration


// 实现数据共享与自定义服务的视图控制器
// 支持将内容分享至社交媒体、邮件、剪切板等系统服务
struct ActivityView: UIViewControllerRepresentable {
    public typealias UIViewControllerType = UIActivityViewController
    
    let activityItem: [Any]
    let excludedActivityTypes: [UIActivity.ActivityType]?
    let applicationActivities: [UIActivity]?
    let onComplete: (UIActivity.ActivityType?, Bool, [Any]?, Error?) -> Void
    public var onDismiss: () -> Void
    
    public init(activityItem: [Any], 
                excludedActivityTypes: [UIActivity.ActivityType]? = nil, 
                applicationActivities: [UIActivity]? = nil, 
                onComplete: @escaping (UIActivity.ActivityType?, Bool, [Any]?, Error?) -> Void = { _, _, _, _ in }, 
                onDismiss: @escaping () -> Void = {}) {
        self.activityItem = activityItem
        self.excludedActivityTypes = excludedActivityTypes
        self.applicationActivities = applicationActivities
        self.onComplete = { activityType, completed, returnedItems, error in
            onComplete(activityType, completed, returnedItems, error)
            // 完成时，自动调用dismiss
            if completed || activityType == nil {
                onDismiss()
            }
        }
        self.onDismiss = onDismiss
    }
    
    public func makeUIViewController(context: Context) -> UIActivityViewController {
        let activity = UIActivityViewController(activityItems: activityItem, applicationActivities: applicationActivities)
        activity.excludedActivityTypes = excludedActivityTypes
        activity.completionWithItemsHandler = onComplete
        return activity
    }
    
    public func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
        
    }
}

struct ActivityModifier: ViewModifier {
    @Binding var isPresented: Bool
    let activityItem: [Any]
    let excludedActivityTypes: [UIActivity.ActivityType]?
    let applicationActivities: [UIActivity]?
    let onComplete: (UIActivity.ActivityType?, Bool, [Any]?, Error?) -> Void
    
    func body(content: Content) -> some View {
        content
            .sheet(isPresented: $isPresented) { 
                ActivityView(activityItem: activityItem, 
                             excludedActivityTypes: excludedActivityTypes, 
                             applicationActivities: applicationActivities, 
                             onComplete: onComplete) { 
                    isPresented = false
                }
//                .presentationDetents([.medium])
//                .background(.white)
            }
            .edgesIgnoringSafeArea(.all)
    }
}

extension BasicWrapper where Base: View {
    public func acitvityModifier(isPresented: Binding<Bool>,
                     activityItem: [Any],
                     excludedActivityTypes: [UIActivity.ActivityType]? = nil,
                     applicationActivities: [UIActivity]? = nil,
                          onComplete: @escaping (UIActivity.ActivityType?, Bool, [Any]?, Error?) -> Void = { _, _, _, _ in }) -> some View {
        
        base.modifier(
            ActivityModifier(isPresented: isPresented,
                             activityItem: activityItem, 
                             excludedActivityTypes: excludedActivityTypes, 
                             applicationActivities: applicationActivities, 
                             onComplete: onComplete)
        )
    }
}

#endif


